﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Security.Cryptography;

namespace GestorDeFlotasDesktop.AbmUsuario
{
    public partial class UserData : MasterPage
    {
        Form ventanaPadre;
        string userName;
        ArrayList roles;
        private capaDatos.SQLSyntaxisGeneration usuariosCD = new capaDatos.SQLSyntaxisGeneration();

        public UserData(Form ventana_padre, string user_Name, ArrayList roles_)
        {
            InitializeComponent();
            ventanaPadre = ventana_padre;
            userName = user_Name;
            txtNombreUser.Text = userName;
            roles = roles_;
        }

        private void UserData_Load(object sender, EventArgs e)
        {
            ventanaPadre.Visible = false;
            txtNombreUser.Text = userName;
        }

        private void btbConfirma_Click(object sender, EventArgs e)
        {
            DataTable dt1;
            DataTable dt2;

            ArrayList param = new ArrayList();
            param = parametrosStoredProcedure(paramsNames("PreguntaSecreta", "userName"), paramsValues(txtPregSect.Text, txtNombreUser.Text));
            dt1 = usuariosCD.executeSp("PELUSA.Modificar_Preg_Secreta", param);
            param.Clear();
            string rstaEncr = BitConverter.ToString(encriptar(txtRstaSec.Text));
            param = parametrosStoredProcedure(paramsNames("RespuestaSecreta", "userName"), paramsValues(rstaEncr, txtNombreUser.Text));
            dt2 = usuariosCD.executeSp("PELUSA.Modificar_Respuesta_Secreta", param);

            if (Convert.ToString(dt1.Rows[0].ItemArray[0]) == "ok" && Convert.ToString(dt2.Rows[0].ItemArray[0]) == "ok")
            {
                mensajeUpdate();

                this.showForm(new Menu(ventanaPadre, roles));
                this.Close();

            }
            else { mensajeError(); }

        }


        private void UserData_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (txtPregSect.Text != "")
            {
                this.Close();
                this.Visible = false;
                this.showForm(new Menu(ventanaPadre, roles));

            }
        }
    }
}
